//
//  WEXEmoticonKeyBoard.swift
//  microBlog
//
//  Created by apple on 16/4/27.
//  Copyright © 2016年 Hill. All rights reserved.
//

import UIKit

class WEXEmoticonKeyBoard: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        // 点击toolBar按钮要执行的闭包va
        stk_toolBar.clickEmticonButtonClosure = { (type)->() in
            
//            print(type)
//            获取当前按对应的位置
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            self.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
//            设置pageControl的数据显示
            self.showPageControl(indexPath)
        }
        
        // 添加工具栏
        addSubview(stk_toolBar)
        
        // 添加表情界面
        addSubview(emoticonCollectionView)
        
        // 添加分页控制器
        addSubview(pc_pageControl)
        
        // 添加工具栏的约束
        stk_toolBar.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
            
        }
        
        // 表情界面的约束
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(stk_toolBar.snp_top)
            
        }
        
        
        // 分页控制器的约束
        pc_pageControl.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(stk_toolBar.snp_top)
            make.centerX.equalTo(stk_toolBar)
        }
        

    }
    
    // MARK: - 设置子控件布局
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let layout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: SCREENWIDTH, height: self.frame.height - stk_toolBar.frame.height)
        
    }

    
   
    // MARK: - 懒加载子控件
    
    // 表情界面
    private lazy var emoticonCollectionView: UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        layout.scrollDirection = .Horizontal
        
        let emoticon = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        emoticon.backgroundColor = UIColor.clearColor()
        
        emoticon.dataSource = self
        emoticon.delegate = self
        emoticon.registerClass(WEXEmoticonPageCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
//        取消弹簧效果
        emoticon.bounces = false
//        开启分页效果
        emoticon.pagingEnabled = true
//        隐藏滚动条
        emoticon.showsHorizontalScrollIndicator = false

        
        return emoticon
    }()
    
    // 底部的工具栏
    private lazy var stk_toolBar: WEXEmoticonToolBar = {
    
        let toolBar = WEXEmoticonToolBar(frame: CGRectZero)
        
        
        return toolBar
    }()
    
    // 分页控制
    private lazy var pc_pageControl: UIPageControl = {
       
        let page = UIPageControl()
        
        page.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        page.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey:  "_pageImage")
        
        return page
        
    }()
}

// MARK: - 数据源和代理方法
extension WEXEmoticonKeyBoard: UICollectionViewDataSource, UICollectionViewDelegate {
    
//    返回有多少组表情
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return WEXEmoticonKeyBoardViewModel.sharedViewModel.allEmoticonArray.count
    }
    
//    返回每组有多少页
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return WEXEmoticonKeyBoardViewModel.sharedViewModel.allEmoticonArray[section].count
        
    }
//    返回每页的内容
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! WEXEmoticonPageCollectionViewCell
//        设置下标文字
        cell.indexPath = indexPath
        
        cell.emoticons = WEXEmoticonKeyBoardViewModel.sharedViewModel.allEmoticonArray[indexPath.section][indexPath.item]
        
        return cell
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
//        获得空间的中心
        var center = scrollView.center
//        获取移动中的视图x中点
        center.x = center.x + scrollView.contentOffset.x
        
//        获得视图中出现的cell
        let cells = emoticonCollectionView.visibleCells()
        
        let indexPath: NSIndexPath
        
        
        if cells.count == 2 {
            
            let firstCell = cells.first!
            let lastCell = cells.last!
            
            // 前一个参数表示可视cell中心点所在的cell的范围，后一个参数表示中心点
            if CGRectContainsPoint(firstCell.frame, center){
//                获取第一个cell在collectionView中的位置
                indexPath = emoticonCollectionView.indexPathForCell(firstCell)!
            }else{
                
                indexPath = emoticonCollectionView.indexPathForCell(lastCell)!
            }
        }else{
            
            indexPath = emoticonCollectionView.indexPathForCell(cells.first!)!
        }
        
        
        stk_toolBar.selectSection = indexPath.section
        
        showPageControl(indexPath)
        
    }
    
    private func showPageControl(indexPath: NSIndexPath){
        // 更改pageControl的总页数
        pc_pageControl.numberOfPages = WEXEmoticonKeyBoardViewModel.sharedViewModel.allEmoticonArray[indexPath.section].count
//        获取当前所在页
        pc_pageControl.currentPage = indexPath.item
        
        pc_pageControl.hidden = indexPath.section == 0
        
        
    }

}
